package bTree.LeetCode;

import java.util.*;

public class 前K个高频元素 {
    public int[] topKFrequent(int[] nums, int k) {
        HashMap<Integer,Integer>hashMap=new HashMap<>();
        for (int num : nums) {
            if(!hashMap.containsKey(num)){
                hashMap.put(num,1);
            }else {
                hashMap.put(num,hashMap.get(num)+1);
            }
        }
        ArrayList<Map.Entry<Integer,Integer>>arrayList=new ArrayList<>(hashMap.entrySet());
        Collections.sort(arrayList,(o1,o2)->o2.getValue()-o1.getValue());
        int []target=new int[k];
        for (int i = 0; i < k; i++) {
            target[i]=arrayList.get(i).getKey();
        }
        return target;
    }
}